<%@ page import="java.util.HashMap" %>
<%@ page import="courseSystem.bean.CourseBean" %>
<%@ page import="courseSystem.bean.UserBean" %>
<%@ page import="java.util.List" %>
<%@ page contentType="text/html;charset=UTF-8" %>
<%
//    String path = request.getContextPath();
//    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
    List<CourseBean> courses = (List<CourseBean>) request.getSession().getAttribute("findCourses");
    List<CourseBean> myCourses = (List<CourseBean>) request.getSession().getAttribute("myCourses");
    String prompt = (String) request.getSession().getAttribute("prompt");
    UserBean user = (UserBean) request.getSession().getAttribute("user");
    String keyWord = (String) request.getSession().getAttribute("keyWord");
    String export = (String) request.getSession().getAttribute("export");
    String p1 = (String) request.getSession().getAttribute("p1");
    String p2 = (String) request.getSession().getAttribute("p2");
    String[] keys = {"id", "nickname", "name", "classroom", "department"};
    String[] v = {"id", "昵称", "姓名", "教室", "学院"};
    HashMap<String, String> s = user.getUser();
    if (prompt != null) {
        out.println("<script>alert('" + prompt + "');</script>");
        request.getSession().removeAttribute("prompt");
    }
    if (export != null) {
        response.setContentType("application/text;charset=utf-8");
        response.setHeader("Content-Disposition", "attachment;filename=course.txt");
        response.setCharacterEncoding("utf-8");
        out.write(export);
        out.flush();
        out.close();
        request.getSession().removeAttribute("export");
    }
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<%--    <base href="<%=basePath%>">--%>
    <title>学生首页</title>
    <link href="../css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="../css/myCss.css">
    <script type="text/javascript" src="../js/jquery.min.js"></script>
    <script type="text/javascript" src="../js/bootstrap.min.js"></script>
</head>
<body id="bd">
<%!
    private String getCourse(List<CourseBean> list, String prompt, String flag) {
        StringBuilder str = new StringBuilder("<table class=\"table table-bordered table-hover\"><thead><tr><th>编号</th><th>课程名</th><th>教师</th><th>教室</th><th>时间1-56（星期一五六节）</th><th>学分</th><th>操作</th></tr></thead><tbody>");
        for (int i = 0; i < list.size(); i++) {
            HashMap<String, String> map = list.get(i).getCourse();
            str.append("<tr class='").append(i % 2 == 0 ? "info" : "").append("'><td>").append(map.get("course_id")).append("</td><td>").append(map.get("course_name")).append("</td><td>").append(map.get("teacher_name")).append("</td><td>").append(map.get("classroom")).append("</td><td>").append(map.get("course_time")).append("</td><td>").append(map.get("credits")).append("</td><td><a href=courseServlet?flag=").append(flag).append("&course_id=").append(map.get("course_id")).append(">").append(prompt).append("</a></td></tr>");
        }
        str.append("</tbody></table>");
        return str.toString();
    }
%>
<div class="container">
    <div class="row clearfix">
        <div class="col-md-12 column">
            <nav class="navbar navbar-default" role="navigation">
                <div class="navbar-header">
                    <button type="button" class="navbar-toggle" data-toggle="collapse"
                            data-target="#bs-example-navbar-collapse-1"><span
                            class="sr-only">Toggle navigation</span><span class="icon-bar"></span><span
                            class="icon-bar"></span><span class="icon-bar"></span></button>
                    <a class="navbar-brand" href="../courseServlet?flag=all">选课系统</a>
                </div>
                <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
                    <ul class="nav navbar-nav navbar-right">
                        <li class="dropdown">
                            <a href="#" class="dropdown-toggle" data-toggle="dropdown">
                                <%=s.get("name")%>
                                <strong class="caret"></strong></a>
                            <ul class="dropdown-menu">
                                <li><a href="#modal-container-1" data-toggle="modal">修改密码</a></li>
                                <li><a href="#modal-container-2" data-toggle="modal">个人信息</a></li>
                                <li><a href="../courseServlet?flag=export">导出课程</a></li>
                                <li><a href="../doorServlet?flag=logout">退出登录</a></li>
                            </ul>
                        </li>
                    </ul>
                </div>
            </nav>
            <div class="tabbable" id="tabs-355102">
                <ul class="nav nav-tabs">
                    <li><a href="#panel-771787" data-toggle="tab">所有课程</a></li>
                    <li><a href="#panel-413799" data-toggle="tab">已选课程</a></li>
                </ul>
                <div class="tab-content">
                    <div class="tab-pane <%=p1%>" id="panel-771787">
                        <br/><br/>
                        <form action="../courseServlet?flag=find" method="post" class="navbar-form navbar-left"
                              role="search">
                            <div class="form-group">
                                <label for="keyWord">输入课程关键字：</label>
                                <input type="text" id="keyWord" name="keyWord" class="form-control"
                                       value="<%=(keyWord==null?"":keyWord)%>"/>
                            </div>
                            <button type="submit" class="btn btn-sm btn-info">查询</button>
                        </form>
                        <div class="container">
                            <div class="row clearfix">
                                <div class="col-md-12 column">
                                    <%
                                        if (courses == null) {
                                            courses = (List<CourseBean>) request.getSession().getAttribute("allCourses");
                                            out.println(getCourse(courses, "选择", "choose"));
                                        } else if (!courses.isEmpty())
                                            out.println(getCourse(courses, "选择", "choose"));
                                        else
                                            out.print("<h1 align=\"center\">没有查询到课程名中包含“" + keyWord + "”的课程！</h1>");
                                    %>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="tab-pane <%=p2%>" id="panel-413799">
                        <div class="container">
                            <div class="row clearfix">
                                <div class="col-md-12 column">
                                    <%
                                        if (myCourses != null && !myCourses.isEmpty()) {
                                            out.println("<h3>这是您选择的课程：</h3><br><table class=\"table table-bordered table-hover\">");
                                            out.println(getCourse(myCourses, "退课", "withdraw"));
                                        } else out.println("<h2>您还没有选择课程！</h2>");
                                    %>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<div class="modal fade" id="modal-container-1" role="dialog" aria-labelledby="changePass" aria-hidden="true">
    <div class="modal-dialog">
        <div class="form">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">X</button>
                <h3 class="modal-title" id="changePass">修改密码</h3>
            </div>
            <form action="../doorServlet?flag=change" method="post">
                <div class="form-group">
                    <div class="col-xs-12  ">
                        <div class="input-group">
                            <span class="input-group-addon"><span class="glyphicon glyphicon-lock"></span></span>
                            <label for="pass1"></label>
                            <input id="pass1" type="text" name="pass1" class="form-control" placeholder="旧密码"/>
                        </div>
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-xs-12  ">
                        <div class="input-group">
                            <span class="input-group-addon"><span class="glyphicon glyphicon-lock"></span></span>
                            <label for="pass2"></label>
                            <input id="pass2" type="password" name="pass2" class="form-control" placeholder="新密码"/>
                        </div>
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-xs-12  ">
                        <div class="input-group">
                            <span class="input-group-addon"><span class="glyphicon glyphicon-lock"></span></span>
                            <label for="pass3"></label>
                            <input id="pass3" type="password" name="pass3" class="form-control" placeholder="确认密码"/>
                        </div>
                    </div>
                </div>
                <input type="hidden" name="userClass" value="student">
                <div class="form-group form-actions">
                    <div class="col-xs-4 col-xs-offset-4 ">
                        <button type="submit" class="btn btn-primary"><span class="glyphicon glyphicon-ok"></span>提交
                        </button>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>
<div class="modal fade" id="modal-container-2" role="dialog" aria-labelledby="myData" aria-hidden="true">
    <div class="modal-dialog">
        <div class="form">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">X</button>
                <h3 class="modal-title" id="myData">个人信息</h3>
            </div>
            <form action="#" method="post">
                <%
                    for (int i = 0; i < keys.length; i++) {
                        out.println("<div class=\"form-group\"><div class=\"col-xs-12\"><div class=\"input-group\">");
                        out.println("<span class=\"input-group-addon\"><span class=\"glyphicon\">" + v[i] + "</span></span>");
                        out.println("<label for='" + keys[i] + "'></label><input id='" + keys[i] + "' type=\"text\" name='" + keys[i] + "' class=\"form-control\" value='" + s.get(keys[i]) + "' readonly/>");
                        out.println("</div></div></div>");
                    }
                %>
            </form>
        </div>
    </div>
</div>
</body>
</html>
